package lab4;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SimpleIteration extends JFrame {
	private static final long serialVersionUID = 1L;

	// creating of component of interface
	private JLabel lblSlar = new JLabel("SLAR:");
	private JLabel lblVariable = new JLabel("Numbers of variable:");
	private JLabel lblEqal = new JLabel("=");
	private JLabel lblResults = new JLabel("Results:");
	private JLabel lblAccuracy = new JLabel("Accuracy:");

	private JTextField txfAccuracy = new JTextField();

	private JButton btnClear = new JButton("Clear input data");
	private JButton btnResolv = new JButton("Resolv SLAR");

	private JPanel pnlResolv = new JPanel(null);
	private JPanel pnlSlar = new JPanel(null);

	private JTextArea txaResults = new JTextArea();

	private JSpinner jspNomber = new JSpinner();

	TableExModel tblExModelSlar = new TableExModel();
	TableExModel tblExModelRes = new TableExModel();
	Calculation calc = new Calculation();

	private JTable tblSlar = new JTable(tblExModelSlar);
	JScrollPane scrollPaneSlar = new JScrollPane(tblSlar);

	private JTable tblRes = new JTable(tblExModelRes);
	JScrollPane scrollPaneRes = new JScrollPane(tblRes);

	// creating of variables
	private int n = 8;
	private double accuracy = 0.001;
	private double[][] matrSlar = null;
	private double[][] matrRes = null;
	private double[] matrResults = null;
	private String result = System.getProperty("line.separator");

	// interface constructor
	public SimpleIteration(String title) {
		setSize(600, 535);
		setLocation(400, 100);
		setTitle(title);
		setResizable(false);
		setVisible(true);
		setLayout(null);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		tblExModelSlar.fill(n, n);
		tblExModelRes.fill(n, 1);
		jspNomber.setValue(n);

		pnlSlar.setBorder(BorderFactory.createLineBorder(Color.red));
		pnlSlar.setBounds(10, 10, 575, 275);

		lblVariable.setBounds(10, 15, 120, 15);
		lblAccuracy.setBounds(10, 40, 70, 15);
		lblSlar.setBounds(10, 65, 35, 15);
		lblEqal.setBounds(485, 160, 20, 20);

		jspNomber.setBounds(140, 10, 49, 25);

		txfAccuracy.setBounds(140, 40, 50, 20);
		txfAccuracy.setText(Double.toString(accuracy));

		scrollPaneSlar.setBounds(10, 90, 450, 147);
		scrollPaneRes.setBounds(515, 90, 50, 147);

		btnClear.setBounds(10, 245, 555, 20);

		pnlResolv.setBorder(BorderFactory.createLineBorder(Color.red));
		pnlResolv.setBounds(10, 295, 575, 200);

		btnResolv.setBounds(10, 10, 555, 20);
		lblResults.setBounds(10, 30, 50, 35);
		txaResults.setBounds(10, 65, 555, 125);
		txaResults.setColumns(n);
		txaResults.setLineWrap(true);
		txaResults.setWrapStyleWord(true);

		// adding components to panels
		pnlSlar.add(lblVariable);
		pnlSlar.add(lblAccuracy);
		pnlSlar.add(txfAccuracy);
		pnlSlar.add(btnClear);
		pnlSlar.add(jspNomber);
		pnlSlar.add(scrollPaneRes);
		pnlSlar.add(scrollPaneSlar);
		pnlSlar.add(lblEqal);
		pnlSlar.add(lblSlar);
		pnlResolv.add(btnResolv);
		pnlResolv.add(lblResults);
		pnlResolv.add(txaResults);
		// adding panels to main panel
		add(pnlSlar);
		add(pnlResolv);

		matrResults = new double[n];
		// listener to change number of variables of SLAR
		jspNomber.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				if ((Integer) jspNomber.getValue() < 3) {
					jspNomber.setValue(2);
				}
				if ((Integer) jspNomber.getValue() > 8) {
					jspNomber.setValue(8);
				}
				n = (Integer) jspNomber.getValue();
				tblExModelSlar.fill(n, n);
				tblExModelRes.fill(n, 1);

			}
		});

		// listener to button Clear
		btnClear.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				tblExModelSlar.fill(n, n);
				tblExModelRes.fill(n, 1);
				txaResults.setText("");
			}
		});

		// listenr to button Resolv Slar
		btnResolv.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				result = "";
				// get matrix from table
				matrSlar = tblExModelSlar.getMatr();
				matrRes = tblExModelRes.getMatr();
				// checing convegense if slar convergense, resolv it
				if (calc.checkConvergense(matrSlar, n) == true) {
					// call the method for solving SLAR
 					matrResults = calc.resolvSlar(matrSlar, matrRes, n,
							accuracy);
 					//out resulte to screen
					for (int i = 0; i < n; i++) {
						result = "X" + (i + 1) + "=" + matrResults[i]
								+ "        \n";
						txaResults.append(result);
					}
				} else {
					txaResults.setText("Slar don't Convergense\n");

				}
			}
		});
	}

	public static void main(String[] args) {
		new SimpleIteration("SLAR Simple Iteration Method");
	}
}
